<%@page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="com.qidian.common.beans.AccountBean"%>
<%@page import="com.qidian.common.beans.UserBean"%>
<%@page import="com.qidian.util.*"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
	PageList pageList = (PageList)request.getAttribute("pageList");
	List<AccountBean> listAccount = (List<AccountBean>)pageList.getList();
	int leftPoint = ((UserBean)request.getSession().getAttribute("user")).getLeftPoint();

%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <base href="<%=basePath%>">
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>开发者页面</title>
	<meta http-equiv="expires" content="0">    
	<%@ include file="../../common/extjs.jsp" %>
	<script>
	if(typeof String.prototype.trim !== 'function') {
	  String.prototype.trim = function() {
	    return this.replace(/^\s+|\s+$/g, ''); 
	  }
	}
var win;
var winRenew;
var action ;
Ext.onReady(function(){

	var tb = Ext.create('Ext.toolbar.Toolbar');
	tb.render('toolbar');
	tb.add({text: '首页',iconCls: 'update',handler: onIndexClick});
	tb.add({text: '创建子账号',iconCls: 'add',handler: onCreateClick});
	tb.add({text: '创建子账号充值卡',iconCls: 'add',handler: onCreateRechargeCardClick});
	tb.add({text: '用户充值',iconCls: 'add',handler: onUserRechargeClick});
	tb.add({text: '刷新',iconCls: 'refresh',handler: onRefreshClick});
	tb.add({text: '可用点数：<%=leftPoint%>'});
	tb.doLayout();
	if(!win){
        win =  Ext.create('Ext.window.Window', {
        	contentEl: 'dlg',
        	title:'创建',
        	width:240,
        	height:120,
        	autoHeight:true,
        	buttonAlign:'center',
        	closeAction:'hide',
	        buttons: [{text:'提交',handler: function(){valiDataForm();}},
	        	{text:'取消',handler: function(){win.hide();}}]
        });
        win.hide();
    }
	if(!winRenew){
        winRenew =  Ext.create('Ext.window.Window', {
        	contentEl: 'dlgUserRecharge',
        	title:'用户充值',
        	width:240,
        	height:120,
        	autoHeight:true,
        	buttonAlign:'center',
        	closeAction:'hide',
	        buttons: [{text:'提交',handler: function(){UserRecharge();}},
	        	{text:'取消',handler: function(){winRenew.hide();}}]
        });
        winRenew.hide();
    }    
    
});	
function onIndexClick(){
	window.location.href = '<%=basePath%>';
}
function onCreateClick(){
    document.getElementById('dataForm').reset();
    document.getElementById('point').value= '';
    win.setTitle('创建子账号(注册码)');
    action = 'createAccount';
    win.show();
}

function onRefreshClick(){
	window.location.href = 'user?actionType=listAccount';
}
function onCreateRechargeCardClick(){
    document.getElementById('dataForm').reset();
    document.getElementById('point').value= '';
    win.setTitle('创建子账号充值卡');
    action = 'createRechargeCard' ;
    win.show();
}
function onUserRechargeClick(){
    document.getElementById('dataFormUserRecharge').reset();
    document.getElementById('rechargeCode').value= '';
    winRenew.setTitle('用户充值');
    winRenew.show();
}
function valiDataForm()
{
	var point = document.getElementById('point').value.trim();
	if (!valiInteger(point))
	{
		alert('点数必须为数字');
		return ;
	}
	
	Ext.Ajax.request({
		url: 'user',
		method: 'GET',
		params: {
	        'actionType' : action,
	        'point' : point
	    },
		success: function(transport) {
			var result = transport.responseText;
			if (result.length >= 32)
			{
				if (action == 'createAccount')
					alert('新创建的子账号：'+result);
				else
					alert('新创建的充值卡：'+result);
			}
			else
				alert(result);
			win.hide();
  		}
	});
}
function valiInteger(cyclenum){
	var re = /^[1-9]\d*$/;
    if (!re.test(cyclenum)){
        return false;
    }else {
    	return true;
    }
}
function UserRecharge()
{
	var rechargeCode = document.getElementById('rechargeCode').value.trim();
	if (rechargeCode.length == 0)
	{
		alert('请输入充值卡');
		return;
	}
	
	Ext.Ajax.request({
		url: 'user',
		method: 'GET',
		params: {
	        'actionType' : 'userRecharge',
	        'rechargeCode' : rechargeCode
	    },
		success: function(transport) {
			var result = transport.responseText;
			if (result)
			{
				alert('充值点数:'+result);
			}
			else
				alert(result);
			winRenew.hide();
  		}
	});
}
</script>
</head>
<body>
	<div id="toolbar"></div>	
	<form id="listForm" name="listForm" action="" method="get">
	<%=pageList.getPageInfo().getHtml("user?action=listAccount") %>
	<br />
	<input type="hidden" name="user_id" id="user_id">
	<table width="90%" align="center" id="the-table">
		<tr align="center" bgcolor="#ADD8E6" class="b_tr">
			<td>帐号</td>
			<td>创建时间</td>
			<td>可用点数</td>
			<td>操作</td>
		</tr>
<%
	for(AccountBean accountBean:listAccount)
	{
%>	
	<tr align="center">
		<td><%=accountBean.getMaskCode() %></td>
		<td><%=new Date(accountBean.getBirthTime()).toLocaleString() %></td>
		<td><%=accountBean.getLeftPoint() %></td>
		<td><button>操作</button></td>
	</tr>
<%
	}
%>
	</table>
	</form>
	<div id="dlg" class="x-hidden">
		<form id="dataForm" name="dataForm" action="" method="get">
			<input type="reset" style="display: none;">
			<input type="submit" style="display: none;">
			<table border="0" align="center" width="120px" >
				<tr height="30">
					<td nowrap>点数:<input type="text" id="point" name="point" value="" maxlength=10/></td>
				</tr>
				<tr><td>最少为1000点</td></tr>
			</table>
		</form>
	</div>
	<div id="dlgUserRecharge" class="x-hidden">
		<form id="dataFormUserRecharge" name="dataFormUserRecharge" action="" method="get">
			<input type="reset" style="display: none;">
			<input type="submit" style="display: none;">
			<table border="0" align="center" width="120px" >
				<tr height="30">
					<td nowrap>充值卡号:<input type="text" id="rechargeCode" name="rechargeCode" value="" maxlength=48/></td>
				</tr>
			</table>
		</form>
	</div>	
</body>
</html>